//
//  WordListItemView.m
//  Learn
//
//  Created by apple on 2024/4/4.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "WordListItemView.h"
#import "WordListItemCell.h"
#import "WordDetailViewController.h"
#import "BeiDanCiRequest.h"
#import "MJRefresh.h"

@interface WordListItemView ()<UITableViewDelegate,UITableViewDataSource>
/* <#描述#> */
@property (nonatomic, strong) UILabel    * titleLabel;
@property (nonatomic, strong) UITableView    * mTableView;

/* <#描述#> */
@property (nonatomic, strong) NSMutableArray    * dataArray;
@property (nonatomic, assign) NSInteger            page;       //
@end

@implementation WordListItemView
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.whiteColor;
        self.type = type;
        [self createUI];
    }
    return self;
}
- (void)createUI {
    self.page = 1;
//    [self addSubview:self.titleLabel];
    [self addSubview:self.mTableView];
    [self.mTableView.mj_header beginRefreshing];
}
- (UILabel *)titleLabel {
    if(!_titleLabel){
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 28)];
        _titleLabel.backgroundColor = COLOR_APP_BACKGROUND;
        _titleLabel.text = @"单词数量5";
        _titleLabel.textColor = UIColor.blackColor;
        _titleLabel.font = PingFangMediumFont(12);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
- (UITableView *)mTableView {
    if(!_mTableView){
        _mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, self.height) style:UITableViewStyleGrouped];
        _mTableView.backgroundColor = UIColor.whiteColor;
        _mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mTableView.delegate = self;
        _mTableView.dataSource = self;
        [_mTableView registerClass:WordListItemCell.class forCellReuseIdentifier:@"WordListItemCell"];
        
        WS(WeakSelf)
        _mTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            WeakSelf.page = 1;
            [WeakSelf requestData];
        }];
        _mTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            WeakSelf.page ++;
            [WeakSelf requestData];
        }];
//        [MethodTool xy_setContentInsetWithScrollView:_mTableView InserBottom:0];
    }
    return _mTableView;
}

#pragma mark ==================== tabledelegate                ======================
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dic = self.dataArray[section];
    NSArray *words = dic[@"words"];
    return words.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 42;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 51;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    WordListItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WordListItemCell"];
    NSDictionary *dic = self.dataArray[indexPath.section];
    NSArray *words = dic[@"words"];
    [cell updateItemCellModel:words[indexPath.row]];
    return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 42)];
    view.backgroundColor = UIColor.whiteColor;
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, 41, SCREEN_W, 0.5 )];
    
    lineView.backgroundColor = rgbHex(0xe5e5e5, 1);
    [view addSubview:lineView];
    
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(10, 19, 4, 10 )];
    
    leftView.backgroundColor = COLOR_TOPIC_SELECT;
    [view addSubview:leftView];

    NSDictionary *dic = self.dataArray[section];

    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftView.right + 5, 0, 200, 20)];
    timeLabel.centerY = leftView.centerY;
    timeLabel.text = [NSString stringWithFormat:@"%@",dic[@"key"]];
    timeLabel.font = PingFangMediumFont(12);
    timeLabel.textColor = COLOR_NOT_SELECT;
    [view addSubview:timeLabel];

    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 0.01)];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataArray[indexPath.section];
    NSArray *words = dic[@"words"];
    NSDictionary *item = words[indexPath.row];
    WordDetailViewController *deatilVC = [[WordDetailViewController alloc]init];
    deatilVC.wordId = item[@"words_id"];
    deatilVC.isFromList = YES;
    [self.baseVC.navigationController pushViewController:deatilVC animated:YES];
}


- (void)requestData {
    WS(WeakSelf)
    [BeiDanCiRequest request_getWordListType:self.type page:self.page reslut:^(NSMutableArray * _Nonnull array, BOOL isMore, BOOL isRequest) {
        if (isRequest) {
            if (WeakSelf.page == 1) {
                [WeakSelf.dataArray removeAllObjects];
            }
            [WeakSelf.dataArray addObjectsFromArray:array];
            [WeakSelf.mTableView reloadData];
            [WeakSelf.mTableView.mj_header endRefreshing];
            [WeakSelf.mTableView.mj_footer endRefreshing];
            if (!isMore) {
                [WeakSelf.mTableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    }];
}
@end
